<template>
	<view>
		<my-search :bgcolor="'#c00000'" @click='gotoSearch'></my-search>
		<view class="scroll-view-container">
			<!-- 左 -->
			<scroll-view scroll-y="true" class="left-scroll-view" :style="{height: wh}">
				<block v-for="(item,i) in cateList" :key="i">
					<view :class="['left-scroll-view-item',i===active ? ' active' : '']" @click="activeChanged(i)">
						{{item.cat_name}}
					</view>
				</block>
			</scroll-view>
			<!-- 右 -->
			<scroll-view scroll-y="true" :scroll-top="scrollTop" class="right-scroll-view" :style="{height: wh}">
				<view class="cate-lv2" v-for="(v,i2) in cateLevel2" :key="i2">
					<view class="cate-lv2-title">
						/{{v.cat_name}}/
					</view>
					<view class="cate-lv2-list">
						<view class="cate-lv2-item" v-for="(item,i3) in  v.children" @click="gotoGoodList(item)"
							:key="i3">
							<image :src="item.cat_icon"></image>
							<text>{{item.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins: [badgeMix],
		data() {
			return {
				wh: 0,
				active: 0,
				scrollTop: 0,
				cateList: [],
				cateLevel2: []
			};
		},
		onLoad() {
			const sysInfo = uni.getSystemInfoSync()
			this.wh = sysInfo.windowHeight * 2 - 100 + 'rpx'
			this.getCateList()
		},
		methods: {
			getCateList() {
				uni.request({
					url: 'https://api-hmugo-web.itheima.net/api/public/v1/categories',
					method: "GET"
				}).then(res => {
					// console.log(res.data.message)
					this.cateList = res.data.message;
					this.cateLevel2 = res.data.message[0].children
					// console.log(res.data.message[0].children)
				})
			},
			activeChanged(i) {
				this.active = i;
				this.cateLevel2 = this.cateList[i].children;
				this.scrollTop = this.scrollTop === 0 ? 1 : 0;
			},
			gotoGoodList(item) {
				uni.navigateTo({
					url: '/subpkg/good_list/good_list?cid=' + item.cat_id
				})
			},
			gotoSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;

		.left-scroll-view {
			width: 240rpx;

			.left-scroll-view-item {
				height: 120rpx;
				line-height: 120rpx;
				background-color: #f7f7f7;
				font-size: 28rpx;
				text-align: center;

				&.active {
					background-color: #fff;
					position: relative;

					&::before {
						content: '';
						display: block;
						width: 6rpx;
						height: 60rpx;
						background-color: #C00000;
						position: absolute;
						top: 50%;
						left: 0;
						transform: translateY(-50%);
					}
				}
			}
		}

		.right-scroll-view {
			.cate-lv2-title {
				text-align: center;
				padding: 20rpx 0;
				font-size: 30rpx;
				font-weight: bold;
			}

			.cate-lv2-list {
				display: flex;
				flex-wrap: wrap;

				.cate-lv2-item {
					width: 33.33%;
					margin: 10rpx 0;
					display: flex;
					flex-direction: column;
					align-items: center;

					image {
						width: 120rpx;
						height: 120rpx;
					}

					text {
						font-size: 24rpx;
					}
				}
			}
		}
	}
</style>
